package com.fg.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.fg.api.AwsFileObject;
import com.fg.api.AwsS3Controller;
import com.fg.model.ImageVO;

public class TmpFileUploadAction extends FileBaseAction implements Preparable{
	@Autowired
	private AwsS3Controller s3_controller;
	
	private File file;
	private String fileFileName;
	private String fileContentType;
	
	@Override
	public void prepare() throws Exception {

	}
	
	@Override
	public String execute() throws Exception {
		
		System.out.println(file);
		System.out.println(fileFileName);
		System.out.println(fileContentType);

		//파일 정보//
		String upload_path = "images/";
		String session_flag = "f_"+((Long)(new Date()).getTime()).toString();
		String stored_name = session_flag + fileFileName.substring(fileFileName.lastIndexOf("."));
		
		//S3로 업로드//
		AwsFileObject afo = new AwsFileObject();
		afo.setFile(file);
		afo.setContent_type(fileContentType);
		afo.setFile_name(stored_name);
		s3_controller.uplaodFile(afo, upload_path);
		
		//메시지가 업로드 되기 전까지 세션에 저장시키기//
		ImageVO image = new ImageVO();
		image.setImage_path(stored_name);
		
		//stored_name 이 세션에서 file_info를 찾는 flag가 된다. 
		session.put(session_flag, image);
		
		setInputStream(session_flag);
		return SUCCESS;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

}


